/**
 * @author 老刘
 * @desc 首页
 */
import {
	go,
	getCloudFunctionData,
	formatTime,
	wxApiPromise
} from '../../../utils/util.js';
const app = getApp();
Page({
	data: {
		clues: '',
		wxStepInfo: {},
		showLoading: false,
		noticeList: [
			{title: '动画(CSS)', url: 'animate_css'},
			{title: '动画(JS)', url: 'animate_js'},
			{title: '特色组件', url: 'characteristic_components'},
			{title: 'canvas', url: 'canvas'}
		]
	},
	onLoad () {
		this.getTimeState();
	},
	async onShow () {
		if (this.data.wxStepInfo?.time) return;
		await this.getRunData();
	},
	async getRunData () {
		this.setData({
			showLoading: true
		});
		const info = await wxApiPromise(wx.getWeRunData, 'werun', this);
		// 通过云调用直接获取开放数据
		const cloudID = info.cloudID;
		// 仅需空的云函数即可获取
		const resData = await getCloudFunctionData('get_user_info',{
			weRunData: wx.cloud.CloudID(cloudID),
			obj: {
				shareInfo: wx.cloud.CloudID(cloudID)
			}
		});
		const wxStepList = resData.result.event.weRunData.data.stepInfoList.reverse();
		wxStepList.map(item => {
			item.time = formatTime(item.timestamp);
		});
		app.globalData.wxStepList = wxStepList;
		let currentStep = wxStepList[0];
		currentStep.time = formatTime(resData.result.event.weRunData.data.watermark.timestamp);
		this.setData({
			wxStepInfo: currentStep,
			showLoading: false
		});
	},
	getTimeState () {
		// 获取当前时间
		const timeNow = new Date();
		// 获取当前小时
		const hours = timeNow.getHours();
		// 设置默认文字
		let state = ``;
		// 判断当前时间段
		if (hours >= 0 && hours <= 10) {
			state = `早上好!`;
		} else if (hours > 10 && hours <= 14) {
			state = `中午好!`;
		} else if (hours > 14 && hours <= 18) {
			state = `下午好!`;
		} else if (hours > 18 && hours <= 24) {
			state = `晚上好!`;
		}
		this.setData({
			clues: state
		});
	},
	handleGoStepList () {
		go(`/pages/default/step_list/step_list`);
	},
	handleGo (e) {
		const url = e.currentTarget.dataset.url;
		go(`/pages/${url}/index/index`);
	}
});
